<template>

  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keywords" placeholder="权限名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="dataList" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="40">
      </el-table-column>
      <el-table-column prop="name" label="权限名" width="120" sortable>
      </el-table-column>
      <el-table-column prop="url" label="路径" width="120" sortable>
      </el-table-column>
      <el-table-column prop="descs" label="描述" width="120" sortable>
      </el-table-column>
      <el-table-column prop="sn" label="sn" width="280" sortable>
      </el-table-column>
      <el-table-column prop="parent.name" label="父级权限"  sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="total, sizes, prev, pager, next, jumper" @current-change="handleCurrentChange"
                     :page-sizes="[5, 10, 20, 50]"  @size-change="handleSizeChange"
                     :page-size="pageSize" :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!-- 添加和编辑界面 -->
    <el-dialog :title="title" :visible.sync="formVisible" :close-on-click-modal="false">
      <el-form :model="permissionForm" label-width="100px" :rules="permissionFormRules" ref="permissionForm">
        <el-form-item label="权限" prop="name">
          <el-input v-model="permissionForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="路径">
          <el-input v-model="permissionForm.url"></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="permissionForm.descs"></el-input>
        </el-form-item>
        <el-form-item label="sn">
          <el-input v-model="permissionForm.sn"></el-input>
        </el-form-item>
        <el-form-item label="父级权限">
          <el-select v-model="permissionForm.parent" value-key="id" clearable filterable placeholder="请选择">
            <el-option
                v-for="per in pers"
                :key="per.id"
                :label="per.name"
                :value="per">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="formVisible = false">取消</el-button>
        <el-button type="primary" @click.native="formSubmit" :loading="permissionFormLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>

</template>

<script>
import util from "../../common/js/util";
import {addUser, batchRemoveUser} from "../../api/api";

export default {
  name:"Permission",
  data(){
    return {
      query:{
        keywords:"",
      },
      dataList:[],
      listLoading:false,
      sels:[],
      total:0,
      currentPage: 1,
      pageSize:10,
      title:"",
      formVisible:false,
      //编辑界面数据
      permissionForm: {
        id: null,
        name: null,
        url:null,
        descs:null,
        sn:null,
        parent_id:null,
        parent:null
      },
      permissionFormRules: {
        username: [
          { required: true, message: '请输入权限', trigger: 'blur' }
        ]
      },
      permissionFormLoading:false,
      pers:[],
      deptTree:[]
    }
  },
  methods:{
    batchRemove(){
      // 触发批量删除操作
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        // let para = { ids: ids };
        this.$http.patch("per",ids).then((res) => {
          this.listLoading = false;
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.loadListData();
        });
      }).catch(() => {
      });
    },
    formSubmit(){
      this.$refs.permissionForm.validate((valid) => {
        if (valid) {  // 通过了表单验证
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            // 确认框
            this.permissionFormLoading = true;
            this.$http.put("/per",this.permissionForm).then((res) => {
              if(res.data.success){
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
              }else {
                this.$message.error(res.data.message);
              }
              this.formVisible = false;
              this.loadListData();
              this.permissionFormLoading = false;
            });
          });
        }
      });
    },
    search(){
      this.currentPage=1;
      this.loadListData();
    },
    handleAdd(){
      this.title = "新增";
      // 清空表单数据
      this.permissionForm = {
        id: null,
        name: null,
        url:null,
        descs:null,
        sn:null,
        parent_id:null,
        parent:null
      }
      // 打开弹出框
      this.formVisible = true;
    },
    selsChange(sels){
      this.sels = sels;
    },
    //状态显示转换
    formatState: function (row, column) {
      return row.state == 1 ? '启用' : row.state == 0 ? '停用' : '未知';
    },
    handleEdit(index,row){
      this.title = "编辑";
      // 数据的回显
      Object.assign(this.permissionForm,row);
      // 打开弹出框
      this.formVisible = true;
    },
    handleDel(index,row){
      this.$confirm('确认删除吗？', '提示', {
        type:'warning'
      }).then(() => {
        // 确认框
        this.$http.delete("/per/"+row.id).then((res) => {
          if(res.data.success){
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }else {
            this.$message.error(res.data.message);
          }
          this.loadListData();
        });
      });
    },
    loadListData(){
      // 加载列表数据
      this.listLoading=true
      // 封装请求数据
      let query = {
        currentPage:this.currentPage,
        pageSize:this.pageSize,
        keywords:this.query.keywords
      }
      // 发送请求获取列表数据
      this.$http.post("/per",query).then(res => {
        this.dataList = res.data.data.data;
        this.total = res.data.data.count;
        // 关闭加载效果
        this.listLoading = false;
      })
    },
    handleCurrentChange(num){
      this.currentPage = num;
      this.loadListData();
    },
    handleSizeChange(num){
      this.pageSize = num;
      this.loadListData();
    },
    loadPers(){
      this.$http.get("/per").then(res => {
        this.pers = res.data.data;
      })
    },
    // loadDeptTree(){
    //   this.$http.get("/dep/tree").then(res => {
    //     this.deptTree = res.data;
    //   })
    // }
  },
  mounted() {
    this.loadListData();
    this.loadPers();
  }
}


</script>

<style scoped>

</style>